package com.abel.quiz.controller;/**
 * Created by abel.lin on 2017/8/4.
 */

import com.abel.quiz.common.vo.AjaxResp;
import com.abel.quiz.po.QuestionPo;
import com.abel.quiz.po.QuestionPoCriteria;
import com.abel.quiz.services.QuestionService;
import com.abel.quiz.vo.QuestionDomain;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author abel.lin
 * @date_time 2017/8/4 16:52
 * @description
 */
@RequestMapping("admin/question")
@Controller
public class QuestionController extends BaseController {
    @Autowired
    private QuestionService questionService;

    @RequestMapping("list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response){
        ModelAndView view = new ModelAndView("admin/question/list");

        return view;
    }

    @ResponseBody
    @RequestMapping("ajaxDataGrid")
    public String ajaxDataGrid(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute QuestionDomain searchArgs,
            @RequestParam(value = "pageNO", defaultValue = "1")Integer pageNO,
            @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSiz){
        AjaxResp ajax = new AjaxResp(AjaxResp.CodeArgs.OK);
        QuestionPoCriteria query = new QuestionPoCriteria();
        QuestionPoCriteria.Criteria criteria = query.createCriteria();
        if(StringUtils.isNotBlank(searchArgs.getContent())){
            criteria.andContentLike(searchArgs.getContent());
        }
        if(searchArgs.getEnabled() != null){
            criteria.andEnabledEqualTo(searchArgs.getEnabled());
        }
        query.setOrderByClause("id");
        PageInfo pageInfo = questionService.selectPageByExample(query,pageNO, pageSiz);
        ajax.setData(pageInfo.getList());
        ajax.setTotal(pageInfo.getTotal());
        return ajax.toJson();
    }

    @RequestMapping("add")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response){
        ModelAndView view = new ModelAndView("admin/question/add");

        return view;
    }

    @ResponseBody
    @RequestMapping("doSave")
    public String doSave(HttpServletRequest request, HttpServletResponse response,
                         @ModelAttribute QuestionDomain domain){
        AjaxResp ajax = new AjaxResp(AjaxResp.CodeArgs.OK);

        domain.setCreateUser(getCurrentUserName());
        domain.setCreateTime(new Date());
        domain.setUpdateUser(domain.getCreateUser());
        domain.setUpdateTime(domain.getCreateTime());
        if(domain.getEnabled() == null){
            domain.setEnabled(0);
        }
        int result = questionService.insertSelective(domain);
        if(result == 0){
            ajax.setCode(AjaxResp.CodeArgs.NO);
        }
        return ajax.toJson();
    }

    @RequestMapping("edit/{id}")
    public ModelAndView edit(HttpServletRequest request, HttpServletResponse response,
            @PathVariable("id")String id){
        ModelAndView view = new ModelAndView("admin/question/edit");
        QuestionDomain domain = questionService.selectDomainById(id);

        view.addObject("vo",domain);
        return view;
    }

    @ResponseBody
    @RequestMapping("doUpdate")
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,
                         @ModelAttribute QuestionDomain domain){
        AjaxResp ajax = new AjaxResp(AjaxResp.CodeArgs.OK);

        domain.setUpdateUser(getCurrentUserName());
        domain.setUpdateTime(new Date());
        if(domain.getEnabled() == null){
            domain.setEnabled(0);
        }
        int result = questionService.updateByPrimaryKeySelective(domain);
        if(result == 0){
            ajax.setCode(AjaxResp.CodeArgs.NO);
        }
        return ajax.toJson();
    }

    @ResponseBody
    @RequestMapping("doDel")
    public String doDel(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id")String id){
        AjaxResp ajax = new AjaxResp(AjaxResp.CodeArgs.OK);

        int result = questionService.deleteByPrimaryKey(id);
        if(result == 0){
            ajax.setCode(AjaxResp.CodeArgs.NO);
        }
        return ajax.toJson();
    }

}
